import http from '@/utils/httpRequest'
import {tempMenuList} from '../../../public/static/menu'
export default {
  namespace: true,
  state: {
    token: '',
    isCollapse: false,
    menuList: [],
    dictList: [],
    userList: [],
    tbLoading: false,
    isClick: false,
    userInfo: {}
  },
  getters: {
    getToken (state) {
      return state.token
    },
    getDictList: (state) => (type, exclude) => {
      return handleDictList(state, type, exclude)
    },
    getDict (state) {
      return function (type, dicKey) {
        return handleDictList(state, type).find(res => res.dicKey.toString() === dicKey.toString())?.dicValue
      }
    },
    getIsClick (state) {
      return state.isClick
    },
    getUserInfo (state) {
      return state.userInfo
    }
  },
  mutations: {
    UPDATE_TOKEN (state, token) {
      state.token = token
    },
    setMenuList (state, menuList) {
      state.menuList = menuList
    },
    setCollapse (state, isCollapse) {
      state.isCollapse = isCollapse
    },
    setDictList (state, dictList) {
      state.dictList = dictList
    },
    setUserList (state, userList) {
      state.userList = userList
    },
    setTableLoading (state, loading) {
      state.tbLoading = loading
    },
    setIsClick (state, isClick) {
      state.isClick = isClick
    },
    setUserInfo (state, info) {
      state.userInfo = info
    }
  },
  actions: {
    updateToken ({commit}, token) {
      commit('UPDATE_TOKEN', token)
    },
    initMenu ({commit}) {
      // /system/dict/type/optionSelect
      const menuList = localStorage.getItem('menuList')
      if (menuList) {
        commit('setMenuList', JSON.parse(menuList))
      } else {
        commit('setMenuList', tempMenuList)
        // http({
        //   url: http.adornUrl('/getRouters'),
        //   method: 'get',
        //   data: http.adornData({
        //     page: 1,
        //     limit: 999
        //   }, false)
        // }).then(({data}) => {
        //   if (data && data.code === 200) {
        //     commit('setMenuList', data.data)
        //     localStorage.setItem('menuList', JSON.stringify(data.data))
        //   }
        // })
      }
    },
    initUserList ({commit}) {
      http({
        url: http.adornUrl('/system/sysUser/noList'),
        method: 'get',
        data: http.adornData({
          page: 1,
          limit: 999
        }, false)
      }).then(({data}) => {
        if (data && data.code === 200) {
          commit('setUserList', data.data)
        }
      })
    }
  }
}


function handleDictList (state, type, exclude = []) {
  return state.dictList.filter(res => res.type === type && exclude.indexOf(res.dicKey) === -1)
}
